#include<iostream>
#include<algorithm>
using namespace std;
const int N = 1e5 + 10;
int arr[N];
int n, m;
int main()
{
	cin >> n >> m;
	for (int i = 0; i < n; i++) {
		cin >> arr[i];
	}
	int ma = -1e9;
	for (int i = 0; i < n - 2; i++) {
		for (int j = i + 1; j < n - 1; j++) {
			for (int k = j + 1; k < n; k++) {
				int v = arr[i] + arr[j] + arr[k];
				if (v % m == 0) {
					ma = max(ma, v);
				}
			}
		}
	}
	cout << ma << endl;
	return 0;
}